package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.t2.AccessPolicy;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.exception.UserNotLoggedException;
import br.com.telescope.t2.feature.CustomFeature;
import com.google.gson.Gson;
import solutions.ferpa.ztest.rest.RestService;
import solutions.ferpa.ztest.rest.model.res.RestRS;

/**
 * Interface de integração Rest com aplicativos.
 * <p>
 * null
 * <P>
 * Telescope origin: ZTEST.REST
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class Rest extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "ZTEST.REST";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Interface de integração Rest com aplicativos");
    }
    
    
    // Record items
    private DomText aSAction;
    private DomTextLong aSBody;
    
    // Query items
    
    @Override
    public void initAttributes() {
        
        aSAction = new DomText();
        aSAction.context(ctx).name("S_ACTION").label("Service (Action)").unbound(true);
        
        aSBody = new DomTextLong();
        aSBody.context(ctx).name("S_BODY").label("Sercive (body)").unbound(true);
        
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CUSTOM">
    public class FCustom extends CustomFeature {
        
        @Override
        public void initForm() {
            addField(aSAction, "opt");
            addField(aSBody, "opt");
        }
        
        @Override
        public int getAccessPolicy() {
            return AccessPolicy.PUBLIC;
        }
        
        @Override
        public void execute() {
            try {
                custom();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("CUSTOM");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FCustom());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "CUSTOM";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getSAction() {
        return aSAction.getValue();
    }
    
    public void setSAction(String value) {
        this.aSAction.setValue(value);
    }
    
    public String getSBody() {
        return aSBody.getValue();
    }
    
    public void setSBody(String value) {
        this.aSBody.setValue(value);
    }
    // </editor-fold>
    
    /**
     * null.
     */
    public void custom() throws AdsException {
        // <user-code id="20724229" parentId="20724173" hashcode="42acf90a" length="1015">
        // import solutions.ferpa.ztest.rest.RestService;
        // import com.google.gson.Gson;
        // import br.com.telescope.t2.exception.UserNotLoggedException;
        // import solutions.ferpa.ztest.rest.model.res.RestRS;
        
        Gson gson = new Gson();
        RestRS<?> response;
        String json; 
        setShown(true);
        ctx.setContentType("application/json;charset=UTF-8");
        
        try {
            RestService service = null;
            String sAction = ctx.getParameter("S_ACTION", "check_session");
            String sBody = ctx.getParameter("S_BODY");
            service = RestService.createService(ctx, sAction);
            if (service.isRestricted() && !ctx.getSession().isUserLogged()) throw new UserNotLoggedException("Usuário não fez login ou sua sessão expirou.");
            if (sBody != null && !sBody.isEmpty()) service.setJsonRequest(sBody);
            response = service.run();
            json = gson.toJson(response);
        } catch (Throwable e) {
            e.printStackTrace();
            response = new RestRS<String>();
            response.getAlerts().addError(e.getMessage());
            json = gson.toJson(response);
        }
        
        ctx.output(json);
        // </user-code>
    }
    
    /**
     * z Code.
     */
    // <user-code id="20724244" parentId="20724236" hashcode="-1271ae2f" length="33">
    // incluir códigos específicos...
    // </user-code>
}
